package cn.ctodb.openapi.idcard.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.openapi.idcard.service.DataIdcardService;
import cn.ctodb.openapi.idcard.web.rest.util.HeaderUtil;
import cn.ctodb.openapi.idcard.web.rest.util.PaginationUtil;
import cn.ctodb.openapi.idcard.service.dto.DataIdcardDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * REST controller for managing DataIdcard.
 */
@RestController
@RequestMapping("/api")
public class DataIdcardResource {

    private final Logger log = LoggerFactory.getLogger(DataIdcardResource.class);

    private static final String ENTITY_NAME = "dataIdcard";
        
    private final DataIdcardService dataIdcardService;

    public DataIdcardResource(DataIdcardService dataIdcardService) {
        this.dataIdcardService = dataIdcardService;
    }

    /**
     * POST  /data-idcards : Create a new dataIdcard.
     *
     * @param dataIdcardDTO the dataIdcardDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new dataIdcardDTO, or with status 400 (Bad Request) if the dataIdcard has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/data-idcards")
    @Timed
    public ResponseEntity<DataIdcardDTO> createDataIdcard(@Valid @RequestBody DataIdcardDTO dataIdcardDTO) throws URISyntaxException {
        log.debug("REST request to save DataIdcard : {}", dataIdcardDTO);
        if (dataIdcardDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new dataIdcard cannot already have an ID")).body(null);
        }
        DataIdcardDTO result = dataIdcardService.save(dataIdcardDTO);
        return ResponseEntity.created(new URI("/api/data-idcards/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /data-idcards : Updates an existing dataIdcard.
     *
     * @param dataIdcardDTO the dataIdcardDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated dataIdcardDTO,
     * or with status 400 (Bad Request) if the dataIdcardDTO is not valid,
     * or with status 500 (Internal Server Error) if the dataIdcardDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/data-idcards")
    @Timed
    public ResponseEntity<DataIdcardDTO> updateDataIdcard(@Valid @RequestBody DataIdcardDTO dataIdcardDTO) throws URISyntaxException {
        log.debug("REST request to update DataIdcard : {}", dataIdcardDTO);
        if (dataIdcardDTO.getId() == null) {
            return createDataIdcard(dataIdcardDTO);
        }
        DataIdcardDTO result = dataIdcardService.save(dataIdcardDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, dataIdcardDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /data-idcards : get all the dataIdcards.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of dataIdcards in body
     */
    @GetMapping("/data-idcards")
    @Timed
    public ResponseEntity<List<DataIdcardDTO>> getAllDataIdcards(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of DataIdcards");
        Page<DataIdcardDTO> page = dataIdcardService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/data-idcards");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /data-idcards/:id : get the "id" dataIdcard.
     *
     * @param id the id of the dataIdcardDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the dataIdcardDTO, or with status 404 (Not Found)
     */
    @GetMapping("/data-idcards/{id}")
    @Timed
    public ResponseEntity<DataIdcardDTO> getDataIdcard(@PathVariable Long id) {
        log.debug("REST request to get DataIdcard : {}", id);
        DataIdcardDTO dataIdcardDTO = dataIdcardService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(dataIdcardDTO));
    }

    /**
     * DELETE  /data-idcards/:id : delete the "id" dataIdcard.
     *
     * @param id the id of the dataIdcardDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/data-idcards/{id}")
    @Timed
    public ResponseEntity<Void> deleteDataIdcard(@PathVariable Long id) {
        log.debug("REST request to delete DataIdcard : {}", id);
        dataIdcardService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

}
